package com.hw.one.collect.dao;

import com.hw.one.collect.param.SiteAirParam;
import com.hw.one.collect.param.SmallAirParam;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/16 16:01.
 */
@Repository
public class FiveMinDataDao {

    @Resource
    protected JdbcTemplate jdbcTemplate;

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;


    public List<Map<String, Object>> findFiveMinData(String sncodes) {
        String sql = "select t.PM25,t.PM10,t.WD,t.WS,t.HUM,t.TEMP,p.SNCODE from "
                + "TPARAVAL_NEW_REAL t , TGPRSMODULE p where "
                + "t.PARENETID = p.id and p.SNCODE in("+sncodes+")";
        List<Map<String, Object>> res = jdbcTemplate.queryForList(sql);
        return res;
    }

    public List<Map> findFiveMinDataByNewOne(Map<String, Object> paramMap) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

        String tableTime = sdf.format(calendar.getTime());

        List<String> snCodeList = (List<String>) paramMap.get("snCodeList");
        Map newData = mongoTemplateStandardSiteData.findOne(
                new Query(Criteria.where("SNCODE").in(snCodeList)).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), Map.class,
                "T_DATA_SC_MIN_S_COLLECT-" + tableTime);
        // 得到最新时间
        Object timeObj = newData.get("MONITORTIME");

        Criteria criteria = Criteria.where("SNCODE").in(snCodeList).and("MONITORTIME").is(timeObj);
        Query query = new Query(criteria);
        List<Map> returnList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_SC_MIN_S_COLLECT-" + tableTime);
        return returnList;
    }

    public List<Map> findRaiseDustFiveMinDataBySNCodeList(SmallAirParam smallAirParam) {
//        Map newData = mongoTemplateStandardSiteData.findOne(
//                new Query(Criteria.where("SNCODE").in(smallAirParam.getSNCodeList())).with(new Sort(Sort.Direction.DESC, "MONITORTIME")), Map.class,
//                "T_DATA_SC_COLLECT_MIN5");
        // 得到最新时间
//        Object timeObj = newData.get("MONITORTIME");
        List<Map> returnList = new ArrayList<>();
        for (String snCode : smallAirParam.getSNCodeList()) {

            Criteria criteria = Criteria.where("SNCODE").is(snCode);
            Query query = new Query(criteria);
            query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
            Map returnMap = mongoTemplateStandardSiteData.findOne(query, Map.class, "T_DATA_SC_COLLECT_MIN5");
            if (null != returnMap) {
                returnList.add(returnMap);
            }
        }
        return returnList;
    }

}
